﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Fis.Web.Rules.Service
{
    public class BackupRule : Agile.Engine.Data.DataRule
    {
        public override void ExecuteNonTrans()
        {
            var action = Paramater["Action"].ToString();
            DataTable data = null;
            Status = 1;

            switch (action)
            {
                case "get":
                    data = GetInfo();
                    break;
                case "set":
                    data = Agile.Engine.Utils.ZipUtils.UnZipDataTable(Paramater["data"].ToString());
                    SetInfo(data);
                    break;
                case "exec":
                    data = Agile.Engine.Utils.ZipUtils.UnZipDataTable(Paramater["data"].ToString());
                    SetInfo(data);
                    Execute();
                    break;
            }

            if (data != null)
                Result["data"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(data);
        }

        DataTable GetInfo()
        {
            return Model.QueryTable("TXTRA", "SID='_default_backup_'");
        }

        void SetInfo(DataTable data)
        {
            data.Rows[0]["SINDEXDATE"] = DateTime.Now;
            var da = new Agile.Engine.Data.DataAccess() { Context = Model.DataContext, TableName = "TXTRA" };
            da.Update(data.Rows[0]);
        }

        void Execute()
        {
            DataBackup.Create(DataBackup.Config).Backup();
        }
    }
}
